<template>
  <div class="app-container">
    <!-- 主体 -->
    <Find :OriginalFilter="{ static: true }" @findClicked="findClicked" :searchImg="false" :formList="formList1" />
    <el-card style="margin-top: 15px" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 50%">
          <h1 class="H1Size">前推时段门槛</h1>
          <Table
            :isLoading="isLoading"
            @selectValChange="confirmChange5"
            @inputChange="confirmChange5"
            @currentRow="currentRow1"
            :tableData="TimeThresholdMethodsTableDate.lt"
            :tableHeader="tableHeader"
            :TableHeight="100"
            :outTableHeight="20"
          />
        </div>
        <div style="margin-left: 15px; width: 50%">
          <h1 class="H1Size">后拉时段门槛</h1>
          <Table
            :isLoading="isLoading1"
            @selectValChange="confirmChange5"
            @inputChange="confirmChange5"
            @currentRow="currentRow1"
            :tableData="TimeThresholdMethodsTableDate.rt"
            :tableHeader="tableHeader1"
            :TableHeight="100"
            :outTableHeight="60"
          />
        </div>
      </div>
    </el-card>

    <el-dialog v-model="dialogVisible1" width="50%" class="dialog pb0">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform" @findClicked="findClicked1" :searchImg="false" :formList="formList2" />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'DefinitionTime'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { onMounted } from 'vue'
import { TimeThreshold, AddTimeThreshold, selectDateLsit, deleteDate } from '@/api/Control/FactoryControl/DefinitionTime'

const isLoading = ref(false)

const isLoading1 = ref(false)

const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入序号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工序代码',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请工序名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入工序作业天数',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '计算方法',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入备注',
    inputValue: '',
    inputLabel: ''
  },

  {
    type: 'button',
    checkboxLabel: '确认添加'
  }
])

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

// 修改贴合材料
const confirmChange5 = async (row?: any) => {
  row.caluType = row.caluType.selectValue
  const result = await LoginWork.put<any, any>('/bioDeptWrok/timeUpdate', row)
  if (result.code === 200) {
    TimeThresholdMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const currentRow1 = (row: any) => {
  const { caluType, id, ltDays, ltType, remark, seqNo, stepCode, stepName } = row
  deleteDateParmsList.value.caluType = caluType.selectValue
  deleteDateParmsList.value.id = id
  deleteDateParmsList.value.ltDays = ltDays
  deleteDateParmsList.value.ltType = ltType
  deleteDateParmsList.value.remark = remark
  deleteDateParmsList.value.seqNo = seqNo
  deleteDateParmsList.value.stepCode = stepCode
  deleteDateParmsList.value.stepName = stepName
}

// 删除
const deleteDateParmsList = ref<any>({
  caluType: '',
  id: '',
  ltDays: '',
  ltType: '',
  remark: '',
  seqNo: '',
  stepCode: '',
  stepName: ''
})

const deleteDateMethods = async () => {
  const result = await deleteDate(deleteDateParmsList.value)
  if (result.code === 200) {
    TimeThresholdMethods()
    deleteDateParmsList.value.id = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 厂商获取
const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const listDate = ref([])

const selectDateLsitMethods = async () => {
  const result = await selectDateLsit()
  formList2.value[4].selectOptions = EvaluatesTheArrayReturnValue(result)
  listDate.value = EvaluatesTheArrayReturnValue1(result)
}

const Launchform = (row: any) => {
  AddTimeThresholdParms.value.seqNo = Number(row[0].inputValue)
  AddTimeThresholdParms.value.stepCode = row[1].inputValue
  AddTimeThresholdParms.value.stepName = row[2].inputValue
  AddTimeThresholdParms.value.ltDays = Number(row[3].inputValue)
  AddTimeThresholdParms.value.caluType = row[4].selectValue
  AddTimeThresholdParms.value.remark = row[5].inputValue
}

// 数据新增
const AddTimeThresholdParms = ref<any>({
  caluType: '',
  id: '',
  ltDays: 0,
  ltType: '',
  remark: '',
  seqNo: '',
  stepCode: '',
  stepName: ''
})

const findClicked1 = (row: string) => {
  if (row === '确认添加') {
    AddTimeThresholdMethods()
  }
}

const AddTimeThresholdMethods = async () => {
  const result = await AddTimeThreshold(AddTimeThresholdParms.value)
  if (result.code === 200) {
    TimeThresholdMethods()
    deleteDateParmsList.value.id = ''
    deleteDateParmsList.value
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 新增数据状态
const dialogVisible1 = ref(false)

const findClicked = (row: string) => {
  switch (row) {
    case '新增前段时段门槛': {
      selectDateLsitMethods()
      dialogVisible1.value = true
      AddTimeThresholdParms.value.ltType = 1
      break
    }

    case '新增后拉时段门槛': {
      selectDateLsitMethods()
      dialogVisible1.value = true
      AddTimeThresholdParms.value.ltType = 2
      break
    }

    case '删除前段时段门槛': {
      if (deleteDateParmsList.value.id === '') {
        JMessage.warning('请选择需要删除的数据')
      } else {
        handleCloseMateriaLKlc()
      }

      break
    }

    case '删除后拉时段门槛': {
      if (deleteDateParmsList.value.id === '') {
        JMessage.warning('请选择需要删除的数据')
      } else {
        handleCloseMateriaLKlc()
      }

      break
    }
  }
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确认要删除数据吗?')
    .then(async () => {
      deleteDateMethods()
    })
    .catch(() => {})
}

// 前后段数据获取
const TimeThresholdMethodsTableDate = ref({
  lt: [],
  rt: []
})

const TimeThresholdMethods = async () => {
  isLoading.value = true
  isLoading1.value = true
  const result = await TimeThreshold()
  TimeThresholdMethodsTableDate.value.lt = result.lt.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    item.caluType = {
      selectValue: item.caluType || '',
      options: listDate
    }
    return item
  })
  TimeThresholdMethodsTableDate.value.rt = result.rt.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    item.caluType = {
      selectValue: item.caluType || '',
      options: listDate
    }
    return item
  })
  isLoading.value = false
  isLoading1.value = false
}

// 数据获取
onMounted(() => {
  TimeThresholdMethods()

  selectDateLsitMethods()
})

// 表格数据
const formList1 = ref([
  {
    type: 'button',
    checkboxLabel: '新增前段时段门槛'
  },
  {
    type: 'button',
    checkboxLabel: '新增后拉时段门槛'
  },
  {
    type: 'button',
    checkboxLabel: '删除前段时段门槛'
  },
  {
    type: 'button',
    checkboxLabel: '删除后拉时段门槛'
  }
])

const tableHeader = ref([
  {
    label: '序号',
    prop: 'seqNo',
    width: '45'
  },
  {
    label: '工序代码',
    prop: 'stepCode'
  },
  {
    label: '工序名称',
    prop: 'stepName'
  },
  {
    label: '工序作业天数',
    prop: 'ltDays',
    width: '80'
  },
  {
    label: '计算方法',
    prop: 'caluType',
    type: 'select'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])

// 表格数据

const tableHeader1 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    width: '45'
  },
  {
    label: '工序代码',
    prop: 'stepCode'
  },
  {
    label: '工序名称',
    prop: 'stepName'
  },
  {
    label: '工序作业天数',
    prop: 'ltDays',
    width: '80'
  },
  {
    label: '计算方法',
    prop: 'caluType',
    type: 'select'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
</script>

<style lang="less" scoped>
.H1Size {
  font-size: 16px;
}
</style>
